﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Logic.Web;
using LotteryVote.Entity;
using LotteryVote.Logic;
using LotteryVote.Utility;
using LotteryVote.Logic.Exceptions;

namespace LotteryVote
{
    public partial class EditUser : AgentBasePage
    {
        int UserId;
        UserFacade userFacade = new UserFacade();
        User TargetUser;
        protected void Page_Load(object sender, EventArgs e)
        {
            InitParams();
        }

        void InitParams()
        {
            string userStr = Request.QueryString["UserId"];
            if (string.IsNullOrEmpty(userStr) || !int.TryParse(userStr, out UserId)) Response.Redirect("~/");
            TargetUser = userFacade.GetUser(UserId);
            if (TargetUser == null || TargetUser.ParentId != CurrentUser.UserId)
                throw new NoPermissionException("Update User");
            if (!IsPostBack)
                InitUserData(TargetUser);
            BindPackage();
        }
        void BindPackage()
        {
            //TODO:用户分组没完成
            if (CurrentUser.Role != SystemRole.Agent) return;
            Execute(() =>
            {
                if (!IsPostBack)
                {
                    CommisionGroupFacade blCommGroup = new CommisionGroupFacade();
                    ddl_package.DataSource = blCommGroup.GetCommissionGroupBySpecie(LotterySpeciesEnum.VietnamLottery);
                    ddl_package.DataTextField = CommissionGroup.GROUPNAME;
                    ddl_package.DataValueField = CommissionGroup.GROUPID;
                    ddl_package.DataBind();
                    MemberPackageFacade packageFacade = new MemberPackageFacade();
                    var package = packageFacade.GetPackage(TargetUser, LotterySpeciesEnum.VietnamLottery);
                    if (package == null)
                        throw new InvalidDataException("Guest:" + TargetUser.UserId + " 没有越南彩分组!");
                    ddl_package.SelectedValue = package.GroupId.ToString();
                }
            });
        }
        void InitUserData(User user)
        {
            lv_userState.SelectedState = user.UserInfo.State;
            txt_givenCredit.Text = user.UserInfo.GivenCredit.ToString();
            txt_Email.Text = user.UserInfo.Email;
            txt_shareRate.Text = (user.UserInfo.ShareRate * 100).ToString();
            txt_userName.Text = user.UserName;
            txt_name.Text = user.UserInfo.Name;
        }

        public string GetAgentType()
        {
            SystemRole role = CurrentUser.Role + 1;
            return GetGlobalResourceObject("Resource", role.ToString()).ToString();
        }

        protected void btn_submit_Click(object sender, EventArgs e)
        {
            Execute(() =>
            {
                if (!EncryptHelper.Equal(txt_myPassword.Text, CurrentUser.UserInfo.Password))
                {
                    ShowMessage("Error", "密码错误!");
                    return;
                }
                decimal givenCredit = decimal.Parse(txt_givenCredit.Text);
                double shareRate = TargetUser.Role == SystemRole.Guest ? 0 : double.Parse(txt_shareRate.Text).PercentageToDecimal(2);
                UserState state = lv_userState.SelectedState;
                userFacade.UpdateUser(TargetUser, givenCredit, shareRate, state);
                MemberPackageFacade packageFacade = new MemberPackageFacade();
                packageFacade.UpdateMemberPackage(TargetUser, LotterySpeciesEnum.VietnamLottery, int.Parse(ddl_package.SelectedValue));
                ShowMessage("Error", GetGlobalResourceObject("Resource", "Success").ToString());
            });
        }

        void ValidFormItem()
        {
            if (!EncryptHelper.Equal(txt_myPassword.Text, CurrentUser.UserInfo.Password))
                throw new PromptException("Password Error!");
            if (!(txt_givenCredit.Text.IsFloat() || txt_shareRate.Text.IsFloat()))
                throw new PromptException("Not avalidale num!");
        }
    }
}